<template>
  <div class="app-main-container">
    <div class="app-main-title">{{ translate("bannerAdmin") }}</div>
    <div class="doumee-filter">
      <el-select
        v-model="filters.venueId"
        :placeholder="translate('jumpTypePlace')"
        clearable
      >
        <el-option
          v-for="item in relation"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-button v-preventReClick type="primary" @click="getListByFilters">{{
        translate("cz")
      }}</el-button>
      <el-button v-preventReClick @click="clearFilters">{{
        translate("clear")
      }}</el-button>
    </div>
    <div class="doumee-table-buttons">
      <el-button type="primary" @click="toNoticeAdd(0)">{{
        translate("add")
      }}</el-button>
    </div>
    <el-table
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('bannerPic')"
        prop="avatar"
        min-width="200"
      >
        <template slot-scope="scope">
          <el-image
            fit="contain"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]"
          /> </template></el-table-column>
      <el-table-column
        align="center"
        :label="translate('jumpType')"
        prop="content"
        min-width="200"
      />
      <el-table-column
        align="center"
        :label="translate('location')"
        prop="venueName"
        min-width="200"
      />
      <el-table-column
        align="center"
        :label="translate('sort')"
        prop="venueName"
        min-width="200"
      />
      <el-table-column
        align="center"
        :label="translate('createTime')"
        prop="createTime"
        width="200"
      />
      <el-table-column
        :label="translate('handle')"
        width="200"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-preventReClick
              type="text"
              @click="toNoticeAdd('1', scope.row.id)"
            >{{ translate("edit") }}</el-button>
            <el-button
              v-preventReClick
              type="text"
              style="color: red"
              @click="toNoticeDelete(scope.row.id)"
            >{{ translate("del") }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="getNoticeList"
      />
    </div>
    <!-- 新增 编辑 消息 -->
    <el-dialog
      :close-on-click-modal="modalDialog"
      :lose-on-press-escape="modalDialog"
      :title="addEditType ? translate('bannerEdit') : translate('bannerAdd')"
      :visible.sync="showAddNotice"
      @closed="cancelAddNotice"
    >
      <el-form
        ref="ruleForm"
        :model="formAddNotice"
        label-width="auto"
        :rules="rules"
      >
        <el-form-item :label="translate('title')" prop="title">
          <el-input v-model="formAddNotice.title" />
        </el-form-item>
        <el-form-item :label="translate('content')" prop="content">
          <el-input v-model="formAddNotice.content" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item :label="translate('releVenue')" prop="recipientsIds">
          <el-checkbox-group v-model="formAddNotice.recipientsIds">
            <el-checkbox
              v-for="item in relation"
              :key="item.id"
              :label="item.id"
              :value="item.id"
            >{{ item.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="cancelAddNotice">{{
          translate("cancel")
        }}</el-button>
        <el-button
          v-preventReClick
          type="success"
          @click="confirmAddNotice('0')"
        >{{ translate("save") }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { accountList, noticeList, noticeAdd, noticePublish, noticeEdit, noticeParam, noticeDelete, venueList } from '@/api'
export default {
  data() {
    return {
      modalDialog: this.$store.state.app.dialog,
      list: [],
      pagination: {
        page: 1,
        rows: 10,
        firstQueryTime: new Date()
      },
      total: 0,
      filters: {
        title: '',
        type: '2'
      },
      relation: [],
      addEditType: 0,
      showAddNotice: false,
      formAddNotice: {
        title: '',
        content: '',
        recipientsIds: [],
        type: '2'
      },
      recipientsList: [],
      rules: {
        title: [
          { required: true, message: this.translate('titlePlace'), trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: this.translate('contentPlace'), trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        recipientsIds: [
          { type: 'array', required: true, message: this.translate('releVenuePlace'), trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getNoticeList()
    this.getAccountList()
    this.getVenueList()
  },
  methods: {
    getVenueList() {
      venueList({
        param: {}
      }).then(res => {
        this.relation = res.recordList
      })
    },
    getNoticeList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
      }
      noticeList({ pagination, param }).then(res => {
        this.list = res.recordList
        this.total = res.totalCount
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = { title: '', type: '2' }
      this.pagination = {
        firstQueryTime: new Date(),
        page: 1,
        rows: 10
      }
      this.getNoticeList()
    },
    // 筛选项查找
    getListByFilters() {
      this.pagination = {
        firstQueryTime: new Date(),
        page: 1,
        rows: 10
      }
      this.getNoticeList()
    },
    // 获取账号列表
    getAccountList() {
      accountList({}).then(res => {
        this.recipientsList = res.recordList
      })
    },
    // 新增/编辑消息
    toNoticeAdd(type, id) {
      this.addEditType = type
      this.showAddNotice = true
      if (id) {
        noticeParam({ param: { id }}).then(res => {
          res.record.recipientsIds = res.record.recipientsList
          res.record.type = '2'
          this.formAddNotice = res.record
        })
      }
    },
    // 取消新增/编辑
    cancelAddNotice() {
      this.showAddNotice = false
      this.formAddNotice = {
        title: '',
        content: '',
        recipientsIds: [],
        type: '2'
      }
    },
    // 确认新增/编辑
    confirmAddNotice(isPublish) {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const { addEditType } = this
          const param = { ...this.formAddNotice }
          param.isPublish = isPublish
          if (addEditType) {
            noticeEdit({ param }).then(res => {
              this.pagination = {
                page: 1,
                rows: 10,
                firstQueryTime: new Date()
              }
              this.getNoticeList()
              this.showAddNotice = false
            })
          } else {
            noticeAdd({ param }).then(res => {
              this.pagination = {
                page: 1,
                rows: 10,
                firstQueryTime: new Date()
              }
              this.getNoticeList()
              this.showAddNotice = false
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 发布通知
    toNoticePublish(id) {
      noticePublish({ param: { id }}).then(res => {
        this.getNoticeList()
      })
    },
    // 删除通知
    toNoticeDelete(id) {
      noticeDelete({ param: { id }}).then(res => {
        this.getNoticeList()
      })
    }
  }
}
</script>
